package com.xmzs.sd.service.impl;

import com.xmzs.common.core.utils.MapstructUtils;
import com.xmzs.common.core.utils.StringUtils;
import com.xmzs.common.mybatis.core.page.TableDataInfo;
import com.xmzs.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xmzs.sd.domain.SdModelParam;
import com.xmzs.sd.domain.bo.SdModelParamBo;
import com.xmzs.sd.domain.vo.SdModelParamVo;
import com.xmzs.sd.mapper.SdModelParamMapper;
import com.xmzs.sd.service.ISdModelParamService;
import lombok.RequiredArgsConstructor;
import net.sf.jsqlparser.expression.MySQLGroupConcat;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 模型参数信息Service业务层处理
 *
 * @author Lion Li
 * @date 2023-07-16
 */
@RequiredArgsConstructor
@Service
public class SdModelParamServiceImpl implements ISdModelParamService {

    private final SdModelParamMapper baseMapper;

    /**
     * 查询模型参数信息
     */
    @Override
    public SdModelParamVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 根据模型名称查询模型参数信息
     */
    @Override
    public SdModelParamVo queryByModelName(String modelName) {
        return baseMapper.selectVoOne(
            new LambdaQueryWrapper<SdModelParam>()
                .eq(SdModelParam::getModelName, modelName)
                .last("limit 1")
        );
    }

    /**
     * 查询模型参数信息列表
     */
    @Override
    public TableDataInfo<SdModelParamVo> queryPageList(SdModelParamBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SdModelParam> lqw = buildQueryWrapper(bo);
        Page<SdModelParamVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询模型参数信息列表
     */
    @Override
    public List<SdModelParamVo> queryList(SdModelParamBo bo) {
        LambdaQueryWrapper<SdModelParam> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SdModelParam> buildQueryWrapper(SdModelParamBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SdModelParam> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPrompt()), SdModelParam::getPrompt, bo.getPrompt());
        lqw.eq(StringUtils.isNotBlank(bo.getNegativePrompt()), SdModelParam::getNegativePrompt, bo.getNegativePrompt());
        lqw.like(StringUtils.isNotBlank(bo.getModelName()), SdModelParam::getModelName, bo.getModelName());
        lqw.eq(bo.getSteps() != null, SdModelParam::getSteps, bo.getSteps());
        lqw.eq(StringUtils.isNotBlank(bo.getSeed()), SdModelParam::getSeed, bo.getSeed());
        lqw.eq(StringUtils.isNotBlank(bo.getWidth()), SdModelParam::getWidth, bo.getWidth());
        lqw.eq(StringUtils.isNotBlank(bo.getHeight()), SdModelParam::getHeight, bo.getHeight());
        lqw.like(StringUtils.isNotBlank(bo.getSamplerName()), SdModelParam::getSamplerName, bo.getSamplerName());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateIp()), SdModelParam::getUpdateIp, bo.getUpdateIp());
        return lqw;
    }

    /**
     * 新增模型参数信息
     */
    @Override
    public Boolean insertByBo(SdModelParamBo bo) {
        SdModelParam add = MapstructUtils.convert(bo, SdModelParam.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改模型参数信息
     */
    @Override
    public Boolean updateByBo(SdModelParamBo bo) {
        SdModelParam update = MapstructUtils.convert(bo, SdModelParam.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SdModelParam entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除模型参数信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
